package com.mmdccj.yugu.mapper;

import com.mmdccj.yugu.entity.Play;
import com.mmdccj.yugu.entity.Post;
import com.mmdccj.yugu.entity.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: star_
 * @date: 2023-11-25 17:26
 */
@Mapper
public interface UserMapper {
    // 根据账号查询用户
    @Select("select * from user where account = #{account}")
    User getUserByAccount(String account);

    // 根据邮箱查询用户
    @Select("select * from user where email = #{email}")
    User getUserByEmail(String email);

    // 在用户表中插入数据
    @Insert("insert into user (id, account, password, name, phone, email, type, create_time, update_time,introduce) " +
            "values (#{id}, #{account}, #{password}, #{name}, #{phone}, #{email}, #{type}, #{createTime}, #{updateTime},'')")
    void insertUser(User user);

    // 获取某个用户名是否被注册过
    @Select("select COUNT(account) from user where account=#{account}")
    int isUser(String account);

    //根据id获取用户
    @Select("select * from user where id = #{id}")
    User getUserById(String id);

    List<User> getCommentsUsers(List<Post> postList);

    //根据id获取用户名
    @Select("select name from user where id = #{id}")
    String getUserNameById(String id);

    //根据音乐id获取用户id
    @Select("select link.user_id from music m join music_link_singer link on link.music_id = m.music_id and link.music_id = #{musicId}")
    String[] getUserIdByMusicId(String musicId);

    //根据多个音乐id获取用户id
    List<String> getUserIdsByMusicIds(List<String> ids);

    // 更新用户信息
    @Update("UPDATE user SET introduce=#{intro},name=#{name} WHERE id=#{id}")
    int updateUserInfo(String id, String name, String intro);

    List<User> getUsersByIds(String[] ids);

    List<User> getUsersByidList(List<String> ids);

    // 获取公开信息
    @Select("SELECT id,name,avatar,introduce FROM user WHERE id=#{id}")
    User getPublicUserInfo(String id);

    // 根据ids获取用户信息list
    List<User> getPublicUserInfoListByUserIds(List<String> ids);

    // 获取用户头像地址
    @Select("SELECT avatar FROM user WHERE id=#{userID}")
    String getAvatar(String userId);

    // 更新用户和头像地址
    @Update("UPDATE user SET avatar=#{url} WHERE id=#{id}")
    int updateAvatar(String url, String id);

    // 获取收藏某个歌单的最多八个用户
    @Select("SELECT collector FROM collect_music_list WHERE music_list_id=#{musicListId} LIMIT 8")
    List<String> getCollectUserIdsByMusicListId(String musicListId);

    // 更新用户的播放次数
    @Update("UPDATE user_plays SET last_play_date=#{date},play_count=play_count+1 WHERE user_id=#{userId} AND music_id=#{musicId}")
    void updateUserPlays(String userId, String musicId, LocalDateTime date);

    // 检查用户是否为首次播放
    @Select("SELECT COUNT(*) FROM user_plays WHERE user_id=#{userId} AND music_id=#{musicId}")
    int isFirstPlay(String userId, String musicId);

    // 为用户创建新的播放表
    @Insert("INSERT INTO user_plays(user_id,music_id,play_count,last_play_date) VALUES(#{userId},#{musicId},1,#{date})")
    void insertPlay(String userId, String musicId, LocalDateTime date);

    // 获取用户本周内的听歌排行信息
    @Select("SELECT * FROM user_music_play WHERE user_id=#{userId} AND date_add(last_play_date, INTERVAL 7 DAY ) > NOW() ORDER BY play_count DESC LIMIT 10")
    List<Play> getUserWeekPlay(String userId);

    @Select("SELECT * FROM user_music_play WHERE user_id=#{userId}  ORDER BY play_count DESC LIMIT 10")
    List<Play> getUserAllPlay(String userId);

    @Delete("DELETE  FROM user_plays WHERE music_id=#{musicId}")
    void deletePlays(String musicId);

    @Select("SELECT COUNT(*) FROM user WHERE phone=#{phone}")
    int isPhone(String phone);
}
